Database reference guide |
HOME |
WHERE ClauseSummaryThe following topics are covered in this section:
Basic WHERE ClauseThe WHERE clause is used to specify the records that should be included in the Domain, so that: SELECT * FROM Customer WHERE Age > 32; createS a Domain that only includes customers older than 32.
The simplest WHERE clause is: WHERE <Column Name> <Operator> <Data> OperatorsThe basic operators that can be used are: = or EQ - record in column is equal to data. < or LT - record in column is less than data. > or GT - record in column is greater than data. <= or LE - record in column is less than or equal to data. >= or GE - record in column is greater than or equal to data. <> or NE - record in column is not equal to data. For example: SELECT Count(*) FROM Customer WHERE Sex = “F”; will include only females and SELECT Count(*) FROM Customer WHERE Age = 32; will include only people aged 32. NOTIt is possible to find the converse of the records included in the WHERE clause by including the NOT statement before the column name, so that: SELECT * FROM Customer WHERE NOT Sex = “F”; returns
This creates a Domain that includes all customers that are not female. However, the records of unknown or Null value will not be included in the Domain. NULL RecordsTo find records that are unknown or null, the IS NULL statement must be used: SELECT * FROM Customer WHERE Sex IS NULL;
Non-Null RecordsTo find all the records that are not Null the following is used: SELECT * FROM Customer WHERE NOT Sex IS NULL; Multiple WHERE clausesThe statements within the WHERE clause can be logically linked to form more complex domains, using OR and AND to determine how each element is to be combined, so that SELECT * FROM Customer WHERE Sex = “F” AND Age = 21; forms a Domain containing only females aged 21:
The following statement forms a Domain containing all females regardless of age, together with all people aged 21 regardless of gender. SELECT * FROM Customer WHERE Sex = “F” OR Age = 21;
You can link as many statements as you need,as demonstrated below: SELECT * FROM Customer WHERE Sex = “F” AND Age = 21 AND Town = “BRISTOL”; This will produce a Domain containing all females aged 21 living in Bristol.
You can use the OR operator to obtain multiple values from the same column: SELECT * FROM Customer WHERE Sex = “F” AND Age = 21 OR Age = 22 OR Age = 23; The Domain formed will include females aged 21 together with anyone aged 22 or anyone aged 23.
If the required Domain is to contain only females aged 21 to 23 then parentheses should be used to isolate the statements in the clause, for example: SELECT * FROM Customer WHERE Sex = “F” AND( Age = 21 OR Age = 22 OR Age = 23);
IN StatementWhen a WHERE clause, as in the previous example, is to include many values for a single column linked by OR, there are two further operators that can be used to simplify (and speed up) the query. The first of these operators is IN. IN is followed by a list of values to be included in the Domain for that specific column: SELECT Count(*) FROM Customer WHERE Sex = “F” AND Age IN(21,22,23); The list following the IN operator can contain one or many elements, IN lists for text fields should be contained in quotes (""), for example:
Note that:
Is exactly the same as:
BETWEEN StatementThe BETWEEN operator specifies the upper and lower inclusive values of a column's records. SELECT Count(*) FROM Customer WHERE Sex = “F” AND Age BETWEEN 20 and 24; This can also be written as: SELECT Count(*) FROM Customer WHERE Sex = “F” AND Age BETWEEN 20,24; The BETWEEN operator can be used to create more efficient NSQL, so that: SELECT Count(*) FROM Customer WHERE Age >= 20 AND Age <= 30; Could be written: SELECT Count(*) FROM Customer WHERE Age BETWEEN 20 and 30; XBETWEEN StatementXBETWEEN is an alternative form of BETWEEN. This operator is similar to BETWEEN except that it is exclusive. The result set will not include any records that are equal to the values that specify the range. For example: SELECT Count(*) FROM Customer WHERE Age > 20 AND Age < 30; Could be written: SELECT Count(*) FROM Customer WHERE Age XBETWEEN 20 and 30; |
Online & Instructor-Led Courses | Training Videos | Webinar Recordings | ![]() |
|
![]() |
© Alterian. All Rights Reserved. | Privacy Policy | Legal Notice | ![]() ![]() ![]() |